package com.xinai.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.xinai.vo.OutLineStocktaking;
import com.xinai.vo.OutLineStocktakingDetail;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class OutLineStocktakingDetailDao extends BaseDao {

    private static final String TABLE = "OutLineStocktakingDetail";

    public OutLineStocktakingDetailDao(Context context){
        super(context);
    }

    // 列表 返回数据
    public List<Map<String,Object>> find(final String name) {
        return callBack(TYPE_READ, new DaoCallBack<List<Map<String,Object>>>() {
            @Override
            public List<Map<String,Object>> invoke(SQLiteDatabase conn) {
                cursor = conn.rawQuery("select * from OutLineStocktakingDetail    where relationNo = ? ", new String[] {name});
                List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
                while (cursor.moveToNext()) {
                    Map<String,Object> map = new LinkedHashMap<>();
                    map.put("id",cursor.getInt(cursor.getColumnIndex("id")));
                    map.put("barcode",cursor.getString(cursor.getColumnIndex("barcode")));
                    map.put("productID",cursor.getString(cursor.getColumnIndex("productID")));
                    map.put("productNo",cursor.getString(cursor.getColumnIndex("productNo")));
                    map.put("productName",cursor.getString(cursor.getColumnIndex("productName")));
                    map.put("colorID",cursor.getString(cursor.getColumnIndex("colorID")));
                    map.put("colorName",cursor.getString(cursor.getColumnIndex("colorName")));
                    map.put("sizesID",cursor.getString(cursor.getColumnIndex("sizesID")));
                    map.put("sizesName",cursor.getString(cursor.getColumnIndex("sizesName")));
                    map.put("Quantity",cursor.getString(cursor.getColumnIndex("Quantity")));
                    list.add(map);
                }
                return list;
            }
        });
    }

    // 列表 返回数据 根据部门 筛选
    public List<Map<String,Object>> findDept(final String name,final String deptid) {
        return callBack(TYPE_READ, new DaoCallBack<List<Map<String,Object>>>() {
            @Override
            public List<Map<String,Object>> invoke(SQLiteDatabase conn) {
                cursor = conn.rawQuery("select a.* from OutLineStocktakingDetail a where Exists(select No from OutLineStocktaking b where a.relationNo=b.No and b.departmentID='"+deptid+"') and a.relationNo in("+name+")  ",null);
                List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
                while (cursor.moveToNext()) {
                    Map<String,Object> map = new LinkedHashMap<>();
                    map.put("id",cursor.getInt(cursor.getColumnIndex("id")));
                    map.put("barcode",cursor.getString(cursor.getColumnIndex("barcode")));
                    map.put("productID",cursor.getString(cursor.getColumnIndex("productID")));
                    map.put("productNo",cursor.getString(cursor.getColumnIndex("productNo")));
                    map.put("colorID",cursor.getString(cursor.getColumnIndex("colorID")));
                    map.put("colorName",cursor.getString(cursor.getColumnIndex("colorName")));
                    map.put("sizesID",cursor.getString(cursor.getColumnIndex("sizesID")));
                    map.put("sizesName",cursor.getString(cursor.getColumnIndex("sizesName")));
                    map.put("Quantity",cursor.getString(cursor.getColumnIndex("Quantity")));
                    list.add(map);
                }
                return list;
            }
        });
    }



   //写入数据
    public int insert(final OutLineStocktakingDetail OutLineStocktaking) {
        return callBack(TYPE_WRITE, new DaoCallBack<Integer>() {
            @Override //replace
            public Integer invoke(SQLiteDatabase conn) {
                conn.execSQL("insert into OutLineStocktakingDetail([relationNo],barcode,productID,productNo,productName,colorID,colorName,sizesID,sizesName,quantity) values(?,?,?,?,?,?,?,?,?,?)", new Object[] {OutLineStocktaking.getRelationNo(), OutLineStocktaking.getBarcode(), OutLineStocktaking.getProductID(),OutLineStocktaking.getProductNo(),OutLineStocktaking.getProductName(),OutLineStocktaking.getColorID(),OutLineStocktaking.getColorName(),OutLineStocktaking.getSizesID(),OutLineStocktaking.getSizesName(),OutLineStocktaking.getQuantity()});
                return 1;
            }
        });
    }

 //删除记录
 public int delete(final String no){

        return callBack(TYPE_WRITE,new DaoCallBack<Integer>() {
            @Override
            public Integer invoke(SQLiteDatabase conn) {

                conn.delete(TABLE," relationNo = ? ",new String[]{no});

                return 1;
            }
        });
 }



}
